var mongoose = require('mongoose')//mongoose是mongodb的ODM,即对象文件映射。可以将数据库的文档转换为JavaScript对象，然后可以进行数据库的增删改查
var Schema = mongoose.Schema
var ObjectId = Schema.Types.ObjectId

var MovieSchema = new Schema({
	doctor:String,
	title:String,
	language:String,
	country:String,
	summary:String,
	flash:String,
	poster:String,
	year:Number,
	pv:{
		type:Number,
		default:0
	},
	category:{
		type:ObjectId,
		ref:'Category'
	},
	meta:{
		createAt:{
			type:Date,
			default:Date.now()
		},
		updateAt:{
			type:Date,
			default:Date.now()
		}
	}
})

MovieSchema.pre('save',function(next){
	if (this.isNew) {
		this.meta.createAt = this.meta.updateAt = Date.now()
	}
	else{
		this.meta.updateAt = Date.now()
	}
	next()
})

MovieSchema.statics ={//这个fetch方法没写对导致我开始查不出数据来，数据库操作静态方法
	fetch: function(cb){
		return this
			.find({})
			.sort({'meta.updateAt':-1})
			.exec(cb)
	},
	finById: function(id,cb){
		return this
			.findOne({_id: id})
			.exec(cb)
	}
}

module.exports = MovieSchema